package com.mycampus.crawler.ruc;

import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.CollectionUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.utils.HttpConstant;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RucPageProcessor implements PageProcessor {

    String detailUrlPrefix = "https://rdjy.ruc.edu.cn/f/recruitmentinfo/ajax_show?ts=";
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);

    @Override
    public void process(Page page) {

        String url = page.getUrl().get();
        System.out.println(url);

        String rawText = page.getRawText();
        System.out.println(rawText);

        if (url.startsWith("https://rdjy.ruc.edu.cn/f/recruitmentinfo/ajax_frontRecruitinfo")) {
            doProcessListPage(page);
        } else {
            doProcessDetail(page);
        }
    }

    private void doProcessDetail(Page page) {

        RucDetailModel rucDetailModel = JSON.parseObject(page.getRawText(), RucDetailModel.class);
        page.putField("originUrl", page.getUrl().get());
        page.putField("title", rucDetailModel.getObject().getRecruitmentinfo().getTitle());
        page.putField("content", rucDetailModel.getObject().getRecruitmentinfo().getContent());
    }

    private void doProcessListPage(Page page) {

        RucListModel listModel = JSON.parseObject(page.getRawText(), RucListModel.class);

        RucListModel.ObjectDTO dto = listModel.getObject();

        Integer pageNo = dto.getPageNo();

        Integer totalPage = dto.getTotalPage();

        if (pageNo < totalPage) {
            addNextListRequest(page, pageNo);
        }

        List<RucListModel.ObjectDTO.ListDTO> list = dto.getList();

        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        for (RucListModel.ObjectDTO.ListDTO d : list) {
            addNewDetailRequest(page, d);
        }
    }

    private void addNewDetailRequest(Page page, RucListModel.ObjectDTO.ListDTO d) {
        String url = detailUrlPrefix + System.currentTimeMillis();
        Request request = new Request(url);
        Map<String, Object> paramMap = new HashMap<>();
        String id = d.getId();
        paramMap.put("recruitmentId", id);
        request.setRequestBody(HttpRequestBody.form(paramMap, "utf-8"));
        request.setMethod(HttpConstant.Method.POST);
        page.addTargetRequest(request);
    }

    private void addNextListRequest(Page page, Integer pageNo) {
        Request request = new Request("https://rdjy.ruc.edu.cn/f/recruitmentinfo/ajax_frontRecruitinfo?ts=" + System.currentTimeMillis());
        request.setMethod(HttpConstant.Method.POST);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageNo", pageNo + 1);
        paramMap.put("pageSize", "15");
        paramMap.put("positionType", "1");
        request.setRequestBody(HttpRequestBody.form(paramMap, "utf-8"));
        page.addTargetRequest(request);
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Request request = new Request("https://rdjy.ruc.edu.cn/f/recruitmentinfo/ajax_frontRecruitinfo?ts=" + System.currentTimeMillis());
        request.setMethod(HttpConstant.Method.POST);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageNo", "1");
        paramMap.put("pageSize", "15");
        paramMap.put("positionType", "1");
        request.setRequestBody(HttpRequestBody.form(paramMap, "utf-8"));

        Spider.create(new RucPageProcessor()).addPipeline(new JsonFilePipeline("/Users/qinyujie3")).addRequest(request).thread(1).run();
    }
}
